package retrofitdemo.com.night.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polyline;

import java.util.ArrayList;
import java.util.List;

import retrofitdemo.com.night.R;
import retrofitdemo.com.night.bean.Location;
import retrofitdemo.com.night.databinding.ActivityGaodeMapHandBinding;
import retrofitdemo.com.night.utils.GsonUtil;
import retrofitdemo.com.night.utils.LogUtil;
import retrofitdemo.com.night.utils.PermissionUtil;

import static retrofitdemo.com.night.bean.ApiService.LOGINPRESSION;
import static retrofitdemo.com.night.bean.ApiService.REQEEST_CDOE_GPS;

public class GaoDeMapActivity extends BaseActivity implements View.OnClickListener {

    Location location = new Location();
    AMapLocationClient mLocationClient = null;
    MyAMapLocationListener listener = new MyAMapLocationListener();
    MapView mMapView = null;
    AMap aMap;
    ActivityGaodeMapHandBinding binding;
    public List<LatLng> listBGps = new ArrayList<>();//点击的gps标点集合
    public List<Polyline> listLine = new ArrayList<>();//线段集合
    public List<Marker> listMarker = new ArrayList<>();//线段集合
    boolean isOver = false;
    String landMaster, landMasterPhone;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_gaode_map_hand);
        binding.setActivity(this);
        mMapView = findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        aMap.setTrafficEnabled(true);
        aMap.setMapType(AMap.MAP_TYPE_SATELLITE);
        aMap.addMarker(new MarkerOptions()
                .position(new LatLng(39.986919,116.353369))
                .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                        .decodeResource(getResources(),R.drawable.ic_launcher_foreground)))
                .draggable(true));

        setBack();
        setTitleText("路线规划");
        initPermission();

        landMaster = getIntent().getStringExtra("module_landmaster");
        landMasterPhone = getIntent().getStringExtra("module_landmaster_phone");
        try {
            setTitleText(landMaster);
        } catch (Exception e) {
            LogUtil.e("intent传递错误");
        }
    }

    private void initPermission() {
        if (PermissionUtil.loginPermission(this, LOGINPRESSION)) {
            if (!PermissionUtil.isOPen(GaoDeMapActivity.this)) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, REQEEST_CDOE_GPS);
            } else {
                initLocation();
            }
        }
    }

    int a = 0;

    //初始化地图
    private void initLocation() {
        mLocationClient = new AMapLocationClient(getApplicationContext());
        mLocationClient.setLocationListener(listener);
        //设置单次定位
        //设置多次定位，时间为1000毫秒
        //        mLocationOption.setInterval(1000);
        mLocationClient.startLocation();
        //        if (aMap == null) {
        //            aMap = mMapView.getMap();
        //        }
        //
        //        MyLocationStyle myLocationStyle;
        //        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        //        myLocationStyle.myLocationType((MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE));//连续定位
        //        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        //        myLocationStyle.strokeColor(getResources().getColor(R.color.A1E0F2));
        //        myLocationStyle.strokeWidth(1);
        //        myLocationStyle.radiusFillColor(getResources().getColor(R.color.HALFA1E0F2));
        //
        //        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        //        aMap.setMapType(AMap.MAP_TYPE_SATELLITE);
        //        aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示，非必需设置。
        //        aMap.setMyLocationEnabled(true);
        //        aMap.moveCamera(CameraUpdateFactory.zoomTo(15));
    }


    class MyAMapLocationListener implements AMapLocationListener {
        @Override
        public void onLocationChanged(final AMapLocation aMapLocation) {
            location.setLatitude(aMapLocation.getLatitude());
            location.setLongitude(aMapLocation.getLongitude());
            location.setAddress(aMapLocation.getAddress());
            location.setCountry(aMapLocation.getCountry());
            location.setCity(aMapLocation.getCity());
            location.setDistrict(aMapLocation.getDistrict());
            location.setStreet(aMapLocation.getStreet());
            location.setStreetNum(aMapLocation.getStreetNum());
            location.setCityCode(aMapLocation.getCityCode());
            location.setAdCode(aMapLocation.getAdCode());
            location.setPoiName(aMapLocation.getPoiName());
            location.setAoiName(aMapLocation.getAoiName());
            location.setErrorCode(aMapLocation.getErrorCode());
            String result = GsonUtil.BeanToJson(location);
            LogUtil.e(result);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onClick(View view) {

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case LOGINPRESSION:
                initGPS();
                break;
            case REQEEST_CDOE_GPS:
                initLocation();
                break;
            default:
        }
    }

    public void initGPS() {
        if (!PermissionUtil.isOPen(this)) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(intent, REQEEST_CDOE_GPS);
        } else {
            initLocation();
        }
    }
}
